/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package startupcompanymanager2012;

import java.awt.Color;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JOptionPane;

/**
 *
 * @author Matthäus
 */
public class SideProject {

    public static boolean projectStatus;
    private int status, totalLines, completedLines, bugs, maxTime, usedTime, maxBugs;
    private double earnings;
    private static Random ran = new Random();
    private static DecimalFormat fmt = new DecimalFormat("0.##");
    private int[] lines = new int[10];

    public SideProject() {
        setLines();
        //chooses randomly a number from the lines array
        this.totalLines = lines[ran.nextInt(10)];
        this.projectStatus = false;
        //calculates the standardbugs for the main developer
        int standardbugs = (int) (totalLines * (Developer.BUGS_PER_LINE - Developer.BUGS_PER_LINE * (Core.devs.get(0).getBugsPerLine() / 100)));
        //calculates the maximum bugs according to the standardbugs(maxbugs<standardbugs)
        this.maxBugs = (int) ((standardbugs) - (standardbugs * ((ran.nextInt(9) + 1) / 10.0)));
        //calulates the time for the sideproject, not allways possible to for developer
        this.maxTime = (int) (0.9 * ((totalLines / (Developer.getMAX_LINES_PER_DAY() * ((double) (Core.devs.get(0).getSpeed()) / 100)))
                + ((standardbugs - maxBugs))));
        //calulates the earnings according to bugs and time for each project
        this.earnings = (double) (totalLines / (Math.sqrt(maxBugs + maxTime)));

    }

    private void setLines() {
        //sets up an array with 10 different integer values for the amount of lines in the project
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                lines[i] = 1000;
            } else {
                lines[i] = lines[i - 1] + 1000;
            }
        }


    }

    public void codeSideProject(int days) {

        if (usedTime + days > maxTime) {
            //Warning if used time + input exceeds max time
            JOptionPane.showMessageDialog(null, "You entered too many days, "
                    + "\nyou are exceeding the maxim amount of days for this project. "
                    + "\nRemember that you need time to debug too.");
        } else {
            //checks if input in relation to remaining lines is ok
            if ((completedLines + days * (Developer.getMAX_LINES_PER_DAY() * ((Core.devs.get(0).getSpeed() / 100.00))))
                    <= (totalLines + (Developer.getMAX_LINES_PER_DAY() * (Core.devs.get(0).getSpeed() / 100.00)))) {
                usedTime += days;
                projectCalendar.addDays(days);
                completedLines += days * (Developer.getMAX_LINES_PER_DAY() * ((Core.devs.get(0).getSpeed() / 100.00)));
                bugs = (int) (completedLines * ((Developer.BUGS_PER_LINE - Developer.BUGS_PER_LINE * (Core.devs.get(0).getBugsPerLine() / 100))));
            } else {

                JOptionPane.showMessageDialog(null, "Your entered too many days in comparison"
                        + "\nto the amount of lines you need to write");
            }
            if (completedLines >= totalLines) {
                /*
                 * if completed lines exceed or equal totallines they are set equal
                 * and the project worker is set into debugging mode
                 */
                completedLines = totalLines;
                Core.sideProjectStatus = Color.orange;
                StartupCompanyManager2012SideProjcWork.buttonWork = false;
                StartupCompanyManager2012SideProjcWork.buttonDebug = true;

            }

        }
    }

    public void debugProject(int days) {
        //debuggs project
        bugs = bugs - (int) (days * ((Developer.BUGS_PER_LINE * 1000.00) * (Core.devs.get(0).getBugsPerLine() / 100.00)));
        projectCalendar.addDays(days);
        usedTime += days;

    }

    public void checkProject() {

        if (completedLines == totalLines) {
            //checks status of project during each debugging-cycle
            if (usedTime < maxTime && bugs <= maxBugs) {
                /*
                 * if used time less then maxtime and 
                 * bugs are lower then max bugs the project is completed
                 * successfull and together with the normal earnings a bonus is
                 * added to the capital
                 */
                StartupCompanyManager2012SideProjcWork.updateStats();
                //if bugs go below 0, they are set to 0

                if (bugs < 0) {
                    bugs = 0;
                }
                //earnings and statistics updated, booleans reset
                Core.runningSideProject = false;
                int daysfaster = maxTime - usedTime;
                int addearnings = daysfaster * 50;
                Core.capital += (earnings + addearnings);
                StartupCompanyManager2012View.updateBudgetLabel();
                Core.sideProjectStatus = Color.green;
                StartupCompanyManager2012SideProjcWork.buttonDebug = false;
                Core.sideProjectFinished = true;
                StartupCompanyManager2012CInfo.spFinished += 1;
                StartupCompanyManager2012CInfo.spCount += 1;
                StartupCompanyManager2012CInfo.spBonus += addearnings;
                StartupCompanyManager2012CInfo.spBugs += bugs;
                StartupCompanyManager2012CInfo.spEarnings += earnings;
                StartupCompanyManager2012CInfo.spLines += completedLines;
                StartupCompanyManager2012CInfo.spDays += usedTime;
                JOptionPane.showMessageDialog(null, "You have successfully finished this side project!"
                        + "\nFor writing " + completedLines + " lines in "
                        + usedTime + " days, \nwith " + bugs + " bugs, you earned "
                        + Core.nfmt.format(earnings) + " \nand additionaly " + Core.nfmt.format(addearnings)
                        + " for completing the project " + daysfaster + " days ahead of time.");
                Core.calendarTimer.start();
            }
            if (usedTime >= maxTime && bugs <= maxBugs) {
                /*
                 * if used time is only a little bigger then maxtime and 
                 * bugs are lower then max bugs the project is completed
                 * successfull.
                 */
                usedTime = maxTime;
                StartupCompanyManager2012SideProjcWork.updateStats();
                //if bugs go below 0, they are set to 0
                if (bugs < 0) {
                    bugs = 0;
                }
                //updates all statistics, adds earnings and resets booleans
                Core.runningSideProject = false;
                Core.capital += earnings;
                StartupCompanyManager2012View.updateBudgetLabel();
                Core.sideProjectStatus = Color.green;
                StartupCompanyManager2012SideProjcWork.buttonDebug = false;
                StartupCompanyManager2012CInfo.spFinished += 1;
                StartupCompanyManager2012CInfo.spCount += 1;
                StartupCompanyManager2012CInfo.spBugs += bugs;
                StartupCompanyManager2012CInfo.spEarnings += earnings;
                StartupCompanyManager2012CInfo.spLines += completedLines;
                StartupCompanyManager2012CInfo.spDays += usedTime;

                Core.sideProjectFinished = true;
                JOptionPane.showMessageDialog(null, "You have successfully finished this side project!"
                        + "\nFor writing " + completedLines + " lines in "
                        + usedTime + " days, \nwith " + bugs + " bugs, you earned "
                        + Core.nfmt.format(earnings) + ".");
                Core.calendarTimer.start();

            }

            if (usedTime >= maxTime && bugs > maxBugs) {
                /*
                 * if used time is bigger or equal to maxtime and the bugs 
                 * are more then the maxbugs, then the project failed and
                 * you have to pay the project owner for his expenses.
                 * 
                 * Statistics are updated, costs subtracted and booleans reset
                 */
                StartupCompanyManager2012SideProjcWork.updateStats();
                double budgetloss = earnings / 10.0;
                Core.runningSideProject = false;
                StartupCompanyManager2012CInfo.spFailed += 1;
                StartupCompanyManager2012CInfo.spCount += 1;
                StartupCompanyManager2012CInfo.spBugs += bugs;
                StartupCompanyManager2012CInfo.spLosses += budgetloss;
                StartupCompanyManager2012CInfo.spDays += usedTime;
                StartupCompanyManager2012CInfo.spLines += completedLines;

                JOptionPane.showMessageDialog(null, "You where not able to finishe the project on time, \n you have to pay "
                        + Core.nfmt.format(budgetloss) + " to compensate the project owners expenses.");

                Core.capital -= budgetloss;
                StartupCompanyManager2012View.updateBudgetLabel();
                StartupCompanyManager2012SideProjcWork.buttonDebug = false;
                Core.sideProjectFinished = true;
                Core.calendarTimer.start();
            }
        }
    }
    //GETTERS

    public int getTotalLines() {

        return totalLines;
    }

    public int getMaxBugs() {

        return maxBugs;
    }

    public int getMaxTime() {

        return maxTime;
    }

    public double getEarnings() {

        return earnings;
    }

    public boolean getProjectStatus() {

        return projectStatus;
    }

    public int getIntStatus() {
        return status = (completedLines / totalLines) * 100;

    }

    public int getCompletedlines() {
        return completedLines;
    }

    public int getBugs() {
        return bugs;
    }

    public int getUsedtime() {
        return usedTime;
    }

    public String toString() {
        String summary = "Lines: " + totalLines + ",   Maximum Bugs: " + maxBugs
                + ",   Time Limit: " + maxTime + " days,   Earnings: " + Core.nfmt.format(earnings);
        return summary;
    }
}
